using System;
using System.Collections.Generic;
using System.Web.Security;
using System.Web.UI.WebControls;
using SouthernLand.Common;

namespace SouthernLand.WebMaster.Membership
{
    public partial class EditUser : BasePage
    {
        private string userName = "";

        protected void Page_Load(object sender, EventArgs e)
        {
            // retrieve the username from the querystring
            userName = Request.QueryString["UserName"];
            //lblRolesFeedbackOK.Visible = false;
            lblProfileFeedbackOK.Visible = false;

            if (!IsPostBack)
            {
                //UserProfile1.UserName = userName;
                // show the user's details
                MembershipUser user = System.Web.Security.Membership.GetUser(userName);
                lblUserName.Text = user.UserName;
                //lnkEmail.Text = user.Email;
                //lnkEmail.NavigateUrl = "mailto:" + user.Email;
                txtEmail.Text = user.Email;
                lblRegistered.Text = user.CreationDate.ToString("f");
                lblLastLogin.Text = user.LastLoginDate.ToString("f");
                lblLastActivity.Text = user.LastActivityDate.ToString("f");
                chkOnlineNow.Checked = user.IsOnline;
                chkApproved.Checked = user.IsApproved;
                chkLockedOut.Checked = user.IsLockedOut;
                chkLockedOut.Enabled = user.IsLockedOut;
                BindRoles();
            }
        }

        private void BindRoles()
        {
            // fill the CheckBoxList with all the available roles, and then select
            // those that the user belongs to
            chklRoles.DataSource = Roles.GetAllRoles();
            chklRoles.DataBind();
            foreach (string role in Roles.GetRolesForUser(userName))
                chklRoles.Items.FindByText(role).Selected = true;
        }

        protected void btnUpdateProfile_Click(object sender, EventArgs e)
        {
            // first remove the user from all roles...
            string[] currRoles = Roles.GetRolesForUser(userName);
            if (currRoles.Length > 0)
                Roles.RemoveUserFromRoles(userName, currRoles);
            // and then add the user to the selected roles
            var newRoles = new List<string>();
            foreach (ListItem item in chklRoles.Items)
            {
                if (item.Selected)
                    newRoles.Add(item.Text);
            }
            Roles.AddUserToRoles(userName, newRoles.ToArray());
            //lblRolesFeedbackOK.Visible = true;
            //UserProfile1.SaveProfile();
            MembershipUser u;
            u = System.Web.Security.Membership.GetUser(userName);
            lblProfileFeedbackOK.Visible = true;
            u.Email = txtEmail.Text;
            System.Web.Security.Membership.UpdateUser(u);
        }

        protected void btnCreateRole_Click(object sender, EventArgs e)
        {
            if (!Roles.RoleExists(txtNewRole.Text.Trim()))
            {
                Roles.CreateRole(txtNewRole.Text.Trim());
                BindRoles();
            }
        }

        protected void chkApproved_CheckedChanged(object sender, EventArgs e)
        {
            MembershipUser user = System.Web.Security.Membership.GetUser(userName);
            user.IsApproved = chkApproved.Checked;
            System.Web.Security.Membership.UpdateUser(user);
        }

        protected void chkLockedOut_CheckedChanged(object sender, EventArgs e)
        {
            if (!chkLockedOut.Checked)
            {
                MembershipUser user = System.Web.Security.Membership.GetUser(userName);
                user.UnlockUser();
                chkLockedOut.Enabled = false;
            }
        }
    }
}